﻿var Sys_ModuleList;
var Sys_ModuleStore;
var Sys_ModuleExtPermissionParentId;  //扩展权限Id
var Sys_ModuleTreeStore;
var Sys_ModuleParentId
// 功能模块grid展示
Sys_ModuleList = function(id) {
Sys_ModuleParentId = id;
    var url = "/Manager/Sys_Module/List/" + id;
    var sm = Ext.create('Ext.selection.CheckboxModel');
    var ModuleEdtId = 0;
    //grid 填充
    Ext.define('Sys_ModuleGridModel', {
        extend: 'Ext.data.Model',
        fields: [
            { name: "ModuleID", type: "string" },
            { name: "M_ParentID", type: "string" },
            { name: "M_PageCode", type: "string" },
            { name: "M_CName", type: "string" },
            { name: "M_Directory", type: "string" },
            { name: "M_OrderLevel", type: "string" },
            { name: "M_IsSystem", type: "string" },
            { name: "M_Close", type: "string" },
            { name: "M_Icon", type: "string" }
            ]
    });

    //grid获取 json数组
    Sys_ModuleStore = Ext.create('Ext.data.Store', {
        pageSize: CMN.pageSize,
        model: 'Sys_ModuleGridModel',
        buffered: false,
        proxy: {
            actionMethods: 'post',
            type: 'ajax',
            url: url,
            reader: {
                type: 'json',
                root: 'root',
                totalProperty: 'totalPorperty'
            }
        },
        // autoLoad: true,
        sorters: [{
            property: 'ModuleID',
            direction: 'DESC'
}]
        });

        //分页设置
        var pagingBar = Ext.create('Ext.PagingToolbar', {
            displayInfo: true, emptyMsg: "没有数据显示", displayMsg: "显示从{0}条数据到{1}条数据，共{2}条数据",
            store: Sys_ModuleStore,
            pageSize: CMN.pageSize,
            //显示右下角信息
            prevText: "上一页",
            nextText: "下一页",
            refreshText: "刷新",
            lastText: "最后页",
            firstText: "第一页",
            beforePageText: "当前页",
            afterPageText: "共{0}页"
        });

        //删除选中行信息
        DeleteSys_ModuleFn = function() {
            var row = Ext.getCmp("Sys_ModulePanel").getSelectionModel().getSelection();
            if (row.length == 0) {
                Ext.Msg.alert("提示信息", "请您至少选择一个!");
            }
            else if (row.length > 1) {
                var idList = "";
                for (var i = 0; i < row.length; i++) {
                    if (i == 0) {
                        idList = row[0].data.ModuleID;
                    }
                    else {
                        idList = idList + "_" + row[i].data.ModuleID;
                    }
                }
                //Ext.Msg.alert("提示信息", "对不起只能选择一个!");
                Ext.Msg.confirm("提 示!", "您确定要删除" + idList + "吗?", function(btn) {
                    if (btn == "yes") {
                        Ext.Ajax.request({
                            url: "/Manager/Sys_Module/DeleteList/" + idList,
                            method: "get",
                            success: function(d) {
                                //成功后
                                var action = Ext.JSON.decode(d.responseText);
                                //alert(action.success);
                                var flag = action.success;
                                if (flag == "true") {
                                    Sys_ModuleStore.loadPage(1);
                                    Ext.MessageBox.alert("恭喜", "删除成功!");
                                }
                                else {
                                    var message = action.message;
                                    if (message) {
                                        Ext.MessageBox.alert("删除失败!", message);
                                    }
                                    else {
                                        Ext.MessageBox.alert("提示!", "删除失败!");
                                    }
                                }
                            },
                            failure: function(form, action) {
                                Ext.MessageBox.alert("提示!", "修改失败!");
                            }
                        })
                    }
                    else {

                    }
                })

            }
            else if (row.length == 1) {
                Ext.Msg.confirm("提 示!", "您确定要删除" + row[0].data.ModuleID + "吗?", function(btn) {
                    if (btn == "yes") {
                        Ext.Ajax.request({
                            url: "/Manager/Sys_Module/Delete/" + row[0].data.ModuleID,
                            method: "POST",
                            success: function(d) {
                                //成功后
                                var action = Ext.JSON.decode(d.responseText);
                                //alert(action.success);
                                var flag = action.success;
                                if (flag == "true") {
                                    Sys_ModuleStore.loadPage(1);
                                    Sys_ModuleTreeStore.load();
                                    Ext.MessageBox.alert("恭喜", "删除成功!");
                                }
                                else {
                                    var message = action.message;
                                    if (message) {
                                        Ext.MessageBox.alert("删除失败!", message);
                                    }
                                    else {
                                        Ext.MessageBox.alert("提示!", "删除失败!");
                                    }
                                }
                            },
                            failure: function(form, action) {
                                Ext.MessageBox.alert("提示!", "修改失败!");
                            }
                        })
                    }
                    else {

                    }
                })
            }
        }


        //修改选中行信息
        EditSys_ModuleFn = function() {
            var row = Ext.getCmp("Sys_ModulePanel").getSelectionModel().getSelection();
            if (row.length == 0) {
                Ext.Msg.alert("提示信息", "您没有选中任何行!");
            }
            else if (row.length > 1) {
                Ext.Msg.alert("提示信息", "对不起只能选择一个!");
            } else if (row.length == 1) {
                EditSys_Module(row[0].data.ModuleID);
            }

        }

        //新增信息
        SaveSys_ModuleFn = function() {
            InsertSys_Module();
        }


        ShowSys_ModuleExtPermissionWin = function(extId) {
        Sys_ModuleExtPermissionParentId = extId;
        Sys_ModuleExtPermissionWin = new Ext.Window({
            title: '扩展权限管理',
            width: 600,
            height: 450,
           
            layout: "fit",
            //iconCls: "addicon",
            //不可以随意改变大小
            resizable: false,
            //是否可以拖动
            //draggable:false,
            defaultType: "textfield",
            //labelWidth: 100,
            collapsible: true, //允许缩放条
            closeAction: 'destroy',
            closable: true,
            //弹出模态窗体
            modal: 'true',
            buttonAlign: "center",
            bodyStyle: "padding:10px 0 0 15px",
            loader: {
            url: '/Manager/Sys_ModuleExtPermission/Index/' + Sys_ModuleExtPermissionParentId,
                contentType: 'html',
                autoLoad: true,
                scripts: true,
                scope: this,
                loadMask: true
            }
        });
       
            Sys_ModuleExtPermissionWin.show();
        }
        //grid配置
        var grid = Ext.create('Ext.grid.Panel', {
            id: "Sys_ModulePanel",
            //renderTo: "Sys_ModuleGrid",
            contentEl: 'Sys_ModuleGrid',
            layout: "fit",
            alias: 'widget.methodPanel',
            columnLines: true,
            loadMask: true,
            frame: true,
            height: Ext.get("MainTab").getHeight() - 30,
            width: 600,
            border: false,
            selModel: sm,   //选择框

            autoScroll: true,
            store: Sys_ModuleStore,
            ds: Sys_ModuleStore,
            //sm: sm,
            columnLines: true,
            //cm: cm2,
            viewConfig: {
                stripeRows: true,
                trackOver: true,
                emptyText: '<div style="color:#999;margin:5px;">当前没有记录显示<div>'
            },
            stripeRows: true,
            listeners: {
                "itemdblclick": function(g, rowIndex, e) {
                    EditSys_ModuleFn();
                }
            },
            bbar: pagingBar,
            tbar: [            		 //工具条设置
            		 {
            		 text: "新增",
            		 tooltip: "新增",
            		 iconCls: "addicon",
            		 handler: SaveSys_ModuleFn
           }, "", "-", "", {
               text: "编辑",
               tooltip: "编辑",
               iconCls: "editicon",
               handler: EditSys_ModuleFn
           }, "", "-", "", {
               text: "删除",
               tooltip: "删除",
               iconCls: "deleteicon",
               handler: DeleteSys_ModuleFn
           }, "-"],
            columns: [
                { header: "ID", dataIndex: "ModuleID", width: 40, sortable: true },
                { header: "父级模块", dataIndex: "M_ParentID", width: 60, sortable: true },
                { header: "模块编码", dataIndex: "M_PageCode", width: 60, sortable: true },
                { header: "模块名称", dataIndex: "M_CName", width: 80, sortable: true },
                
                { header: "是否系统模块", dataIndex: "M_IsSystem", width: 100, renderer: function(value) { if (value == "1") { return "是"; } else { return "否"; } }, sortable: true },
                { header: "是否关闭", dataIndex: "M_Close", width: 60, renderer: function(value) { if (value == "1") { return "是"; } else { return "否"; } }, sortable: true },
                { header: "扩展权限", dataIndex: "ModuleID", renderer: function(value) { return "<a href='javascript:void(0);' title='扩展权限' onclick='ShowSys_ModuleExtPermissionWin(" + value + ")' >管理扩展权限</a>" }, width: 100 },
                { header: "删除", dataIndex: "ModuleID", renderer: function(value) { return "<a href='javascript:void(0);' title='删除' onclick='DeleteSys_ModuleFn()'class='deleteBtn' >&nbsp;&nbsp;&nbsp;</a>" }, width: 40 }
                
             ]

        });


        // tree start create('Ext.Viewport'

        Sys_ModuleTreeStore = Ext.create('Ext.data.TreeStore', {
            fields: ['id', 'text', 'url'],
            proxy: {
                type: 'ajax',
                url: '/Manager/Sys_Module/List/m'
            },
            root: {
                text: '系统菜单',
                id: "0",
                url: "",
                expanded: true
            },
            folderSort: true,
            sorters: [{
                property: 'text',
                direction: 'ASC'
}]
            });

            var tree = Ext.create('Ext.tree.Panel', {
                region: 'west',
                store: Sys_ModuleTreeStore,
                contentEl: 'treeSys_Module',
                split: true,
                border: true,
                collapsible: false,
                columnWidth: 1 / 5,
                width: 200,
                height: Ext.get("MainTab").getHeight() - 30,
                minSize: 120,
                maxSize: 360,
                listeners: {
                    'itemclick': function(view, node) {
                        if (node.data.id != "") {
                            Sys_ModuleParentId = node.data.id;
                            url = "/Manager/Sys_Module/List/" + node.data.id;
                            Sys_ModuleStore.proxy.url = url; //将新的地址写在省略号的地方即可
                            Sys_ModuleStore.loadPage(1)
                        }
                    }
                }
            });

            // tree end

            //tree.render();

            var panel = Ext.create('Ext.Panel', {
                baseCls: 'x-plain',
                renderTo: "Sys_ModuleTop",
                margins: '35 5 5 0',
                layout: {
                    type: 'table',
                    columns: 2
                },
                // applied to child components
                defaults: { frame: true, layout: 'anchor',
                    defaults: {
                        anchor: '100%'
                    }
                },
                items: [
            tree,
            grid
            ]
            });

           
            Sys_ModuleStore.loadPage(1);
           
        }
    Sys_ModuleList(0);
